﻿using Ninject;
using Romantics.Domain;
using Romantics.Domain.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Romantics.ModelBinders
{
    public class HikeModelBinder : DefaultModelBinder
    {
        private readonly IKernel _kernel;

        public HikeModelBinder(IKernel kernel)
        {
          _kernel = kernel;
        }

        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (controllerContext == null)
                throw new ArgumentNullException("controllerContext", "controllerContext is null.");
            if (bindingContext == null)
                throw new ArgumentNullException("bindingContext", "bindingContext is null.");

            var hike = (Hike)base.BindModel(controllerContext, bindingContext);

            var _domainRepository = _kernel.Get<IDomainRepository>();

            if (hike.Category != null)
                hike.Category = _domainRepository.Category(hike.Category.ID);

            if (hike.Region != null)
                hike.Region = _domainRepository.Region(hike.Region.ID);

          return hike;
        }
    }
}